import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from entities import *
from Entities import *


def Add(categoryName):
    category = db.GqlQuery("SELECT * FROM Category WHERE CategoryName = :1",categoryName)
    if category.count() == 0:
        category = Category(CategoryName = categoryName)
        category.put()
        return "Complete"
    else:
        return "Duplicate"

def Delete(CategoryName):
    category = db.GqlQuery("SELECT * FROM Category WHERE CategoryName = :1",CategoryName)
    if category.count() == 0:
        return "No ..."
    else:
        Category.delete(category)
        return "Complete"

def Get():
    category_list = []
    categorys = db.GqlQuery("SELECT * FROM Category ORDER BY CategoryName DESC")
    for category in categorys:
        category_dict = {}
        category_dict["CategoryName"] = category.CategoryName
        category_dict["CategoryID"] = category.key().id()
        category_list.append(category_dict)
    tuple_category = tuple(category_list)
    return tuple_category
    
def GetbyName(categoryName):
    categorys = db.GqlQuery("SELECT * FROM Category WHERE CategoryName =:1",categoryName)
    if categorys.count() != 0:
        for category in categorys:
            return category
    else:
        return None
        